
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Testing," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>验收测试 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        验收测试
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Testing/" rel="tag">Ember-Testing</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 3 分钟</span>
    </header>

    <div class="post-content">
      <h1 id="验收测试"><a href="#验收测试" class="headerlink" title="验收测试"></a>验收测试</h1><p>使用<code>ember generate acceptance-test &lt;name&gt;</code>创建一个验收测试，比如:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ember g acceptance-test login</span><br></pre></td></tr></table></figure>

<p>执行完毕命令之后得到如下文件内容：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//tests/acceptance/login-test.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; test &#125; <span class="keyword">from</span> <span class="string">&#x27;qunit&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> moduleForAcceptance <span class="keyword">from</span> <span class="string">&#x27;people/tests/helpers/module-for-acceptance&#x27;</span>;</span><br><span class="line"></span><br><span class="line">moduleForAcceptance(<span class="string">&#x27;Acceptance | login&#x27;</span>);</span><br><span class="line"></span><br><span class="line">test(<span class="string">&#x27;visting /login&#x27;</span>， <span class="function"><span class="keyword">function</span>(<span class="params">assert</span>) </span>&#123;</span><br><span class="line">  visit(<span class="string">&#x27;/login&#x27;</span>);</span><br><span class="line"></span><br><span class="line">  andThen(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  　assert.equal(currentURL()， <span class="string">&#x27;/login&#x27;</span>);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><code>moduleForAcceptance</code>用来启动、终止程序。最后几行<code>test</code>中包含了一个示例。</p>
<p>几乎所有的测试都有一个路由请求，用于和页面交互(通过helper)并检查DOM是否按照期望值进行改变。</p>
<p>举个例子:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">test(<span class="string">&#x27;should add new post&#x27;</span>， <span class="function"><span class="keyword">function</span>(<span class="params">assert</span>) </span>&#123;</span><br><span class="line">  visit(<span class="string">&#x27;/posts/new&#x27;</span>);</span><br><span class="line">  fillIn(<span class="string">&#x27;input.title&#x27;</span>， <span class="string">&#x27;My new post&#x27;</span>);</span><br><span class="line">  click(<span class="string">&#x27;button.submit&#x27;</span>);</span><br><span class="line">  andThen(<span class="function">() =&gt;</span> assert.equal(find(<span class="string">&#x27;ul.posts li:first&#x27;</span>).text()， <span class="string">&#x27;My new post&#x27;</span>));</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>大体意思为:<br>进入路由<code>/posts/new</code>，在输入框<code>input.title</code>填入<code>My new post</code>，点击<code>button.submit</code>，期望的结果是: 在对应列表下<code>ul.posts li.first</code>的文本为<code>My new post</code>.</p>
<h2 id="测试助手"><a href="#测试助手" class="headerlink" title="测试助手"></a>测试助手</h2><p>在测试web应用中的一个主要的问题是，由于代码都是基于事件驱动的，因此他们有可能是<code>异步</code>的，会使代码<code>无序</code>运行。</p>
<p>比如有两个按钮，从不同的服务器载入数据，我们先后点击他们，但可能结果返回的顺序并不是我们点击的顺序。</p>
<p>当你在编写测试的时候，你需要特别注意一个问题，就是你无法确定在发出一个请求后，是否会立刻得到返回的响应。因此，你的断言需要以同步的状态来等待被测试体。例如上面所举的例子，应该等待两个服务器均返回数据后，这时测试代码才会执行其逻辑来检测数据的正确性。</p>
<p>这就是为什么在做断言的时候，Ember测试助手都是被包裹在一个确保同步状态的代码中。这样做避免了对所有这样的代码都去做这样的包裹，并且因为减少了模板代码，从而提高了代码的可读性.</p>
<p>Ember包含多个测试助来辅助进行验收测试。一共有2种类型:异步助手<code>asynchronous</code>和同步助手<code>synchronous</code></p>
<h3 id="异步测试助手"><a href="#异步测试助手" class="headerlink" title="异步测试助手"></a>异步测试助手</h3><p>异步测试助手可以意识到程序中的异步行为，使你可以更方便的编写确切的测试。</p>
<p>同时，这些测试助手会按注册的顺序执行，并且是链式运行。每个测试助手的调用都是在前一个调用结束之后，才会执行下一个。因此，你可以不用当心测试助手的执行顺序。</p>
<ul>
<li><code>click(selector)</code><ul>
<li>点击一个元素，触发该元素所绑定的<code>click</code>事件，返回一个异步执行成功的<code>promise</code>。</li>
</ul>
</li>
<li><code>fillIn(selector， value)</code><ul>
<li>用执行成功的promise值填充到选中的input元素上。使用<code>&lt;select&gt;</code>作为<code>&lt;input&gt;</code>时，记得<code>&lt;select&gt;</code>元素的<code>value</code>是标签<code>&lt;option&gt;</code>所指定的值，并不是<code>&lt;option&gt;</code>标签显示的内容。</li>
</ul>
</li>
<li><code>keyEvent(selector， type， keyCode)</code><ul>
<li>模拟键盘操作。比如选中的元素检测按键<code>keypress</code>，按键按下<code>keydown</code>，按键弹起<code>keyup</code>事件的<code>keyCode</code>。</li>
</ul>
</li>
<li><code>triggerEvent(selector，type，keyCode)</code><ul>
<li>在指定元素上触发给定事件，比如<code>blur</code>、<code>ddlclick</code>等事件…</li>
</ul>
</li>
<li><code>visit(url)</code><ul>
<li>访问路由并返回promise执行成功的结果。</li>
</ul>
</li>
</ul>
<h3 id="同步测试助手"><a href="#同步测试助手" class="headerlink" title="同步测试助手"></a>同步测试助手</h3><p>同步测试助手，当触发时会立即执行并返回结果.</p>
<ul>
<li><code>currentPath()</code><ul>
<li>返回当前路径</li>
</ul>
</li>
<li><code>currentRouteName()</code><ul>
<li>返回当前激活的路由名称</li>
</ul>
</li>
<li><code>currentURL()</code><ul>
<li>返回当前URL</li>
</ul>
</li>
<li><code>find(selector， context)</code><ul>
<li>寻找应用根元素中的指定下上文(可选)的元素。将其域定义到根元素可以有效的避免与测试框架的冲突。当上下文没有指定的时候，会按照默认方式完成。</li>
</ul>
</li>
</ul>
<h3 id="等待助手"><a href="#等待助手" class="headerlink" title="等待助手"></a>等待助手</h3><p> <code>andThen</code>测试助手将会等待所有异步测试助手完成之后再执行.举个例子:</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  tests/acceptance/new-post-appears-first-test.js</span></span><br><span class="line"></span><br><span class="line">tese(<span class="string">&#x27;should add new post&#x27;</span>， <span class="function"><span class="keyword">function</span>(<span class="params">assert</span>) </span>&#123;</span><br><span class="line">  visit(<span class="string">&#x27;/posts/new&#x27;</span>);</span><br><span class="line">  fillIn(<span class="string">&#x27;input.title&#x27;</span>， <span class="string">&#x27;My new post&#x27;</span>);</span><br><span class="line">  click(<span class="string">&#x27;button.submit&#x27;</span>);</span><br><span class="line">  andThen(<span class="function">() =&gt;</span> assert.equal(find(<span class="string">&#x27;ul.posts li:first&#x27;</span>).text()， <span class="string">&#x27;My new post&#x27;</span>));</span><br><span class="line">  &#125;);</span><br></pre></td></tr></table></figure>

<p>首先，我们访问<code>/posts/new</code>地址，在有<code>title</code>css类的<code>input</code>输入框内内填入内容“My new post”，然后点击有CSS类<code>submit</code>的按钮。</p>
<p>等待签名的异步测试助手执行完（具体说，<code>andThen</code>会在路由<code>/posts/new</code>访问完毕，<code>input</code>表单填充完毕，按钮被点击之后）毕后会执行<code>andThen</code>助手。注意<code>andThen</code>助手只有一个参数，这个参数是一个函数，函数的代码是其实测试助手执行完毕之后才执行的代码。</p>
<p>在<code>andThen</code>助手中，我们最后会调用<code>assert.equal</code>断言来判定对应位置的值是否为<code>My new post</code>。</p>
<h3 id="自定义测试助手"><a href="#自定义测试助手" class="headerlink" title="自定义测试助手"></a>自定义测试助手</h3><p>使用命令<code>ember generate test-helper &lt;helper-name&gt;</code>来创建自定义测试助手。</p>
<p>下面的代码是执行命令<code>ember g test-helper shouldHaveElementWithCount</code>得到的测试例子:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//tests/helpers/should-have-element-with-count.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Test.registerAsyncHelper(</span><br><span class="line">    <span class="string">&#x27;shouldHaveElementWithCount&#x27;</span>， <span class="function"><span class="keyword">function</span>(<span class="params">app</span>)</span>&#123;&#125;</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p><code>Ember.Test.registerAsyncHelper</code>和<code>Ember.Test.registerHelper&#39;是当</code>startApp<code>被调用时，会将自定义测试助手注册。两者的区别在于，前者</code>Ember.Test.registerHelper`会在之前任何的异步测试助手运行完成之后运行，并且后续的异步测试助手在运行前都会等待他完成.</p>
<p>测试助手方法一般都会以当前程序的第一个参数被调用，其他参数，比如<code>assert</code>，在测试助手被调用的时候提供。测试助手需要在调用<code>startApp</code>前进行注册，但<code>ember-cli</code>会帮你处理，你不需要担心这个问题。</p>
<p>下面是一个非异步的测试助手:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//tests/helpers/should-have-element-with-count.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Test.registerHelper(<span class="string">&#x27;shouldHaveElementWithCount&#x27;</span>，</span><br><span class="line">  <span class="function"><span class="keyword">function</span>(<span class="params">app， assert， selector， n， context</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">const</span> el = findWithAssert(selector， context);</span><br><span class="line">    <span class="keyword">const</span> count = el.length;</span><br><span class="line">    assert.equal(n， count， <span class="string">&#x27;found $&#123;count&#125; times&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">//shouldHaveElementWithCount(assert， &#x27;ul li&#x27;， 3);</span></span><br></pre></td></tr></table></figure>

<p>下面是一个异步的测试助手:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Test.registerAsynHelper(<span class="string">&#x27;dblclick&#x27;</span>，</span><br><span class="line">  <span class="function"><span class="keyword">function</span>(<span class="params">app， assert， selector， context</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> $el = findWithAssert(selector， context);</span><br><span class="line">    Ember.run(<span class="function">() =&gt;</span> $el.dblclick());</span><br><span class="line">  &#125;</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">//dblclick(assert， &#x27;#persion-1&#x27;)</span></span><br></pre></td></tr></table></figure>

<p>异步测试助手也可以让你将多个测试助手合并为一个.举个例子:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//tests/helpers/add-contact.js</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Test.registerAsyncHelper(<span class="string">&#x27;addContact&#x27;</span>,</span><br><span class="line">  <span class="function"><span class="keyword">function</span>(<span class="params">app，name</span>) </span>&#123;</span><br><span class="line">    fillIn(<span class="string">&#x27;#name&#x27;</span>, name);</span><br><span class="line">    click(<span class="string">&#x27;button.create&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">//addContact(&#x27;Bob&#x27;);</span></span><br><span class="line"><span class="comment">//addContact(&#x27;Dan&#x27;);</span></span><br></pre></td></tr></table></figure>

<p>最后， 别忘了将你的测试助手添加进<code>tests/.jshintrc</code>和<code>tests/helpers/start-app.js</code>中、在<code>tests/.jshintrc</code>中，你需要将其添加进<code>predef</code>块中，不然就会得到jshint测试失败的消息.</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;predef&quot;</span>: [</span><br><span class="line">    <span class="string">&quot;document&quot;</span>,</span><br><span class="line">    <span class="string">&quot;window&quot;</span>,</span><br><span class="line">    <span class="string">&quot;locaiton&quot;</span>,</span><br><span class="line">    ...</span><br><span class="line">    <span class="string">&quot;shouldHaveElementWithCount&quot;</span>,</span><br><span class="line">    <span class="string">&quot;dblclick&quot;</span>,</span><br><span class="line">    <span class="string">&quot;addContact&quot;</span></span><br><span class="line">  ],</span><br><span class="line">  ...</span><br><span class="line">&#125;*</span><br></pre></td></tr></table></figure>

<p>你需要在<code>tests/helpers/start-app.js</code>引入测试助手，这些助手将会被注册到应用中。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> Application <span class="keyword">from</span> <span class="string">&#x27;../../app&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> Router <span class="keyword">from</span> <span class="string">&#x27;../../router&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> config <span class="keyword">from</span> <span class="string">&#x27;../../config/environmnet&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./should-have-element-with-count&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./dblclick&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./add-contact&#x27;</span>;</span><br></pre></td></tr></table></figure>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/testing/introduction/" rel="next" title="测试简介">
          测试简介
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/templetes/%E8%B0%83%E8%AF%95%E5%8A%A9%E6%89%8B/" rel="prev" title="调试助手">
            调试助手
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%AA%8C%E6%94%B6%E6%B5%8B%E8%AF%95"><span class="toc-text">验收测试</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E5%8A%A9%E6%89%8B"><span class="toc-text">测试助手</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%82%E6%AD%A5%E6%B5%8B%E8%AF%95%E5%8A%A9%E6%89%8B"><span class="toc-text">异步测试助手</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%8C%E6%AD%A5%E6%B5%8B%E8%AF%95%E5%8A%A9%E6%89%8B"><span class="toc-text">同步测试助手</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AD%89%E5%BE%85%E5%8A%A9%E6%89%8B"><span class="toc-text">等待助手</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%B5%8B%E8%AF%95%E5%8A%A9%E6%89%8B"><span class="toc-text">自定义测试助手</span></a></li></ol></li></ol></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '验收测试', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
